package com.tangjiali.map_topic5;
/*
需求：使用LinkedHashMap保存学生对象，及其对应的家庭住址；要求：
        1. 学生具有学号、姓名和成绩三个属性；
        2. 其中，学号不能重复；*/

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;

public class Solution {
    public static void main(String[] args) {
//        3. 定义LinkedHashMap对象，键是学生对象，值是地址
        Map<Student,String> students = new LinkedHashMap<>();
//        4. 创建学生对象存入数据
        Student student1 = new Student(001,"熊大",98);
        Student student2 = new Student(002,"熊二",100);
        Student student3 = new Student(003,"熊三",99);
        Student student4 = new Student(001,"熊",98);
        students.put(student1,"纽约客");
        students.put(student2,"北京");
        students.put(student3,"上海");
        students.put(student4,"伦敦");

//        5. 遍历查看结果
        System.out.println(students);   //{{stuNum=1, name='熊大', score=98.0}=伦敦, {stuNum=2, name='熊二', score=100.0}=北京, {stuNum=3, name='熊三', score=99.0}=上海}

    }
}


//        1. 定义学生类包含学号、姓名和成绩三个属性；
class Student{
    private int stuNum;
    private String name;
    private double score;


    public Student() {
    }

    public Student(int stuNum, String name, double score) {
        this.stuNum = stuNum;
        this.name = name;
        this.score = score;
    }

    public int getStuNum() {
        return stuNum;
    }

    public void setStuNum(int stuNum) {
        this.stuNum = stuNum;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }

    @Override
    public String toString() {
        return "{" +
                "stuNum=" + stuNum +
                ", name='" + name + '\'' +
                ", score=" + score +
                '}';
    }
    //        2. 重写hashCode和equals方法，选中学号即可
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return stuNum == student.stuNum;
    }

    @Override
    public int hashCode() {
        return Objects.hash(stuNum);
    }
}